<?php
/**
* phpCan - http://idc.anavallasuiza.com/
*
* phpCan is released under the GNU Affero GPL version 3
*
* More information at license.txt
*/

defined('ANS') or die();
?>

<header class="clear">
	<hgroup>
		<h1><?php echo $table_info['title']; ?></h1>
		<h2><?php
			if ($Vars->get('id')) {
				__e('Editing #%s', implode(', ', $Vars->get('id')));
			} else {
				__e('Creating new');
			}

			if ($Vars->get('relation')) {
				echo '. '.__('Related with %s #%s', $Vars->get('relation'), $Vars->get('relation_id'));
			}
		?></h2>
		
		<?php if (hasText($table_info['description'])) {
			echo '<p>'.$table_info['description'].'</p>';
		} ?>
	</hgroup>

	<nav>
		<?php
		if ($Vars->get('relation')) {
			echo $Html->a(array(
				'text' => __('Only related elements'),
				'href' => path(true, true, 'related_with', $Vars->get('relation'), $Vars->get('relation_id')),
				'class' => 'button',
				'data-icon' => 'link'
			));
			echo $Html->a(array(
				'text' => __('All elements'),
				'href' => path(true, true, 'related_with', $Vars->get('relation'), $Vars->get('relation_id')).get('all', 1, false),
				'class' => 'button',
				'data-icon' => 'link'
			));
		} else {
			echo $Html->a(array(
				'text' => __('List'),
				'href' => path(true, true, 'list'),
				'class' => 'button',
				'data-icon' => 'home'
			));
		}
		echo $Html->a(array(
			'text' => __('New'),
			'href' => path(true, true, 'new').get(),
			'class' => 'button'.($Vars->getRoute(2, 'new') ? ' secondary' : ''),
			'data-icon' => 'plus'
		));
		?>
	</nav>

	<?php $Templates->render('aux-search.php', array('action_url' => path(true, true, 'list'))); ?>
</header>

<form class="edit" action="<?php echo path().get(); ?>" method="post" enctype="multipart/form-data">
	<?php $Templates->render('aux-edit-form.php', array('edit' => $edit)); ?>

	<fieldset class="footer">
	<?php
	echo $Form->hidden($table, 'table');
	echo $Form->hidden($id, 'id');
	echo $Form->hidden($Vars->get('redirect'), 'redirect');

	echo $Form->submit(array(
		'value' => __('Save'),
		'name' => 'phpcan_action[save]',
		'class' => 'right',
		'button' => true,
		'data-icon' => 'disk'
	));

	if ($Vars->get('id')) {
		echo $Form->submit(array(
			'value' => __('Duplicate'),
			'name' => 'phpcan_action[save]',
			'class' => 'right',
			'button' => true,
			'data-icon' => 'disk',
			'onclick' => 'removeId();'
		));

		echo $Form->submit(array(
			'value' => __('Delete'),
			'button' => true,
			'data-icon' => 'trash',
			'class' => 'secondary',
			'action' => array(
				'name' => 'delete',
				'confirm' => __('Do you realy want to delete this?'),
			)
		));
	}

	echo $Form->button(array(
		'value' => __('Reset'),
		'button' => true,
		'class' => 'right',
		'type' => 'reset',
		'data-icon' => 'refresh'
	));
	?>
	</fieldset>
</form>

<script type="text/javascript">
function removeId () {
	$("input[type='hidden'][name$='[action]']").val('insert');
	$("input[type='hidden'][name*='[id]']").remove();
	return true;
}
</script>
